var mongodb = require('./msession');
//评价机构建模模型
function Comment(obj){ 
	this.name=obj.name;  
	this.password = obj.password;
	this.role = obj.role;
	this.orgnization = obj.orgnization;
};

Comment.prototype.save = function(callback){  //The save function of Manager
	var user = {                
		name:       this.name,   
		password:   this.password,
		role:       this.role,
		orgnization:this.orgnization
	};
    
    mongodb.open((err,db)=>{
    	if(err){     
			return callback({back:'数据库打不开'});
		};
		db.collection("users",(err,collection)=>{
			if(err){   
				mongodb.close();
				return callback({back:'数据库集合打不开'});
			};
            collection.insert(user,{  //插入数据user
				safe:true
			},(err,user)=>{
				mongodb.close();   //关闭数据库
				if(err){           //关闭数据库出错
					return callback(err);
				}
				callback(null,user[0])  //至此完全成功
			})
		})
    })
};

Comment.prototype.myProjects = function(orgnization,callback){  //The GetData function of Manager	
	mongodb.open((err,db)=>{
    	if(err){     
			return callback({back:'数据库打不开'});
		};
		db.collection('projects',(err,collection)=>{
			if(err){mongodb.close(); return callback({back:"the error for opening projects"})}
			collection.find({comment:orgnization}).toArray(function(err,docs){
				mongodb.close();
				if(err){
					return callback(err);
				}
				console.log(docs)
				callback(null,docs);
			})
		})
    })
};


module.exports = Comment;  






